package com.dev.sacot41.scviewpager;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * Created by Samuel on 2015-07-06.
 */
public class SCViewPagerProvider extends PageSliderProvider {

    private Context context;
    private ArrayList<SCViewPagerFragment> mFragmentList;
    private int mNumberOfPage = 0;
    private int mBackgroundColor;

    public SCViewPagerProvider(Context context) {
        this.context = context;
        mFragmentList = new ArrayList<>();
    }

    public void setNumberOfPage(int numberOfPage) {
        mNumberOfPage = numberOfPage;
    }

    public void setFragmentBackgroundColor(int color) {
        mBackgroundColor = color;
    }

    @Override
    public int getCount() {
        return mNumberOfPage;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        SCViewPagerFragment fragment = null;
        if (mFragmentList.size() - 1 >= position) {
            fragment = mFragmentList.get(position);
        }
        if (fragment == null) {
            fragment = new SCViewPagerFragment(context);
            fragment.setBackground(mBackgroundColor);
            mFragmentList.add(fragment);
        }
        componentContainer.addComponent(fragment);
        return fragment;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
        componentContainer.removeComponent(mFragmentList.get(position));
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return false;
    }

    public static class SCViewPagerFragment extends DirectionalLayout {

        public SCViewPagerFragment(Context context) {
            super(context);
            setLayoutConfig(new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT));
            setOrientation(VERTICAL);
        }

        public void setBackground(int bgColor) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(bgColor));
            setBackground(shapeElement);
        }

    }

}
